﻿using System.ComponentModel;

namespace Marmat.Forms.Views
{
    namespace Grid
    {
        public partial class Column
        {
            public Column()
            {
                this.InitializeComponent(new DevExpress.XtraGrid.Columns.GridColumn());
            }

            public Column(DevExpress.XtraGrid.Columns.GridColumn gridColumn)
            {
                this.InitializeComponent(gridColumn);
            }

            /// <summary>
            /// 初始化组件
            /// </summary>
            /// <param name="gridColumn"></param>
            private void InitializeComponent(DevExpress.XtraGrid.Columns.GridColumn gridColumn)
            {
                this._gridColumn = gridColumn;
                gridColumn.Tag = this;
                gridColumn.Width = this.Width;
                gridColumn.OptionsColumn.AllowEdit = this.AllowEdit;
                gridColumn.Caption = this.Caption;
                gridColumn.Visible = this.Visible;
                gridColumn.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridColumn.AppearanceHeader.Options.UseTextOptions = true;
            }

            /// <summary>
            /// 获取或设置日期格式。
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            [DefaultValue(typeof(DateTimeFormatEnum), "Date")]
            public DateTimeFormatEnum DateTimeFormat
            {
                get
                {
                    if (this.DisplayFormat.FormatType == DevExpress.Utils.FormatType.DateTime)
                    {
                        switch (this.GridColumn.DisplayFormat.FormatString ?? "")
                        {
                            case "d":
                                return DateTimeFormatEnum.Date;
                            case "G":
                                return DateTimeFormatEnum.DateLongTime;
                            case "g":
                                return DateTimeFormatEnum.DateTime;
                            case "T":
                                return DateTimeFormatEnum.LongTime;
                            case "t":
                                return DateTimeFormatEnum.Time;
                        }
                    }
                    return default;
                }
                set
                {
                    if (value == default(int))
                    {
                        this.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                        this.GridColumn.DisplayFormat.FormatString = null;
                    }
                    else
                    {
                        this.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                        switch (value)
                        {
                            case DateTimeFormatEnum.Date:
                                this.GridColumn.DisplayFormat.FormatString = "d";
                                break;
                            case DateTimeFormatEnum.DateLongTime:
                                this.GridColumn.DisplayFormat.FormatString = "G";
                                break;
                            case DateTimeFormatEnum.DateTime:
                                this.GridColumn.DisplayFormat.FormatString = "g";
                                break;
                            case DateTimeFormatEnum.LongTime:
                                this.GridColumn.DisplayFormat.FormatString = "T";
                                break;
                            case DateTimeFormatEnum.Time:
                                this.GridColumn.DisplayFormat.FormatString = "t";
                                break;
                        }
                    }
                }
            }

            /// <summary>
            /// 获取或设置显示的单元格格式。
            /// </summary>
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            internal DevExpress.Utils.FormatInfo DisplayFormat
            {
                get => this.GridColumn.DisplayFormat;
            }

            /// <summary>
            /// 返回表示当前对象的字符串。
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                if (!string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this._Caption))
                    return this.Name + " [" + this.Caption + "]";
                else if (!string.IsNullOrEmpty(this.Name))
                    return this.Name;
                return base.ToString();
            }
        }
    }
}